package group.sicher.notice.sms.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import group.sicher.notice.sms.Sms;
import group.sicher.notice.sms.exception.SmsConfigException;
import group.sicher.notice.sms.exception.SmsException;
import group.sicher.notice.sms.exception.SmsTemplateNotFoundException;
import lombok.Setter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author Donald
 * @description 阿里云短信推送, 配置信息查看application_notice.properties文件
 * @date 2019-06-26
 **/
@Service("aliSms")
@Setter
@PropertySource(value = "classpath:application_notice.properties", encoding = "UTF-8")
@ConfigurationProperties(prefix = "aliyun.sms", ignoreInvalidFields = true)
public class AliSmsImpl implements InitializingBean, Sms {

    private String domain;
    private String version;
    private String action;
    private String signName;
    private String key;
    private String secret;
    private Map<String, String> templates;

    @Override
    public void afterPropertiesSet() throws Exception {
        if (StringUtils.isEmpty(domain)) {
            throw new SmsConfigException("阿里云短信Domain不能为空");
        }

        if (StringUtils.isEmpty(version)) {
            throw new SmsConfigException("阿里云短信Version不能为空");
        }

        if (StringUtils.isEmpty(action)) {
            throw new SmsConfigException("阿里云短信Action不能为空");
        }

        if (StringUtils.isEmpty(signName)) {
            throw new SmsConfigException("阿里云短信SignName不能为空");
        }

        if (StringUtils.isEmpty(key)) {
            throw new SmsConfigException("阿里云短信Key不能为空");
        }

        if (StringUtils.isEmpty(secret)) {
            throw new SmsConfigException("阿里云短信Secret不能为空");
        }

        if (CollectionUtils.isEmpty(templates)) {
            throw new SmsConfigException("阿里云短信Template不能为空");
        }
    }

    @Override
    public boolean send(String phone, Map<String, String> params, String template) throws SmsException, SmsTemplateNotFoundException {
        if (!templates.containsKey(template)) {
            throw new SmsTemplateNotFoundException(template + "短信模版不存在");
        }

        DefaultProfile profile = DefaultProfile.getProfile("default", key, secret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();

        request.setSysProtocol(ProtocolType.HTTPS);
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(domain);
        request.setSysVersion(version);
        request.setSysAction(action);

        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templates.get(template));
        request.putQueryParameter("TemplateParam", new Gson().toJson(params));

        Map<String, String> data;
        try {
            CommonResponse response = client.getCommonResponse(request);
            data = new Gson().fromJson(response.getData(), new TypeToken<Map<String, String>>() {
            }.getType());
        } catch (ClientException e) {
            throw new SmsException(e.getMessage());
        }

        if (!"ok".equalsIgnoreCase(data.get("Code"))) {
            throw new SmsException(data.get("Message"));
        }

        return true;
    }

}
